import requests
import unittest


class A3mall(unittest.TestCase):
    def setUp(self) -> None:
        print('start case')

    def tearDown(self) -> None:
        print('end case')

    # 注册
    def test_A3mall_01(self):
        register = requests.post(url='http://10.59.9.14:7003/api/register',
                                 json={"username": "18185603006", "password": "123456"})
        print(register.json())

    token = None

    # 登录
    def test_A3mall_02(self):
        login = requests.post(url='http://10.59.9.14:7003/api/public/login',
                              json={"username": "18185603008", "password": "123456"})
        print(login.json())
        A3mall.token = login.json()['data']['token']
        print(A3mall.token)

    # 我的
    def test_A3mall_03(self):
        info = requests.get(url='http://10.59.9.14:7003/api/ucenter/info',
                            headers={'auth-token': 'Bearer ' + A3mall.token})
        print(info.json())

    spid = None

    # 搜索
    def test_A3mall_04(self):
        search = requests.get(
            url='http://10.59.9.14:7003/api/search/list?page=1&keywords=%E5%B0%8F%E7%B1%B3&type=0&sort=1')

        print(search.json())
        # 提取商品id
        A3mall.spid = search.json()['data']['list'][0]['id']
        print('商品id:', A3mall.spid)

    # 商品详情
    def test_A3mall_05(self):
        view = requests.get(url='http://10.59.9.14:7003/api/goods/view?id=' + str(A3mall.spid))
        print(view.json())

    # 添加购物车
    def test_A3mall_06(self):
        add = requests.post(url='http://10.59.9.14:7003/api/cart/add',
                            headers={'auth-token': 'Bearer ' + A3mall.token},
                            json={"id": A3mall.spid, "sku_id": "", "num": 1})
        print(add.json())

    cartid = None

    # 购物车列表
    def test_A3mall_07(self):
        cart = requests.get(url='http://10.59.9.14:7003/api/cart?page=1',
                            headers={'auth-token': 'Bearer ' + A3mall.token})
        print(cart.json())
        # 提取购物车列表的商品ID
        A3mall.cartid = cart.json()['data']['list'][0]['id']
        print(A3mall.cartid)

    # 提交订单
    def test_A3mall_08(self):
        confirm = requests.get(
            url=f'http://10.59.9.14:7003/api/order/confirm?id=' + str(A3mall.cartid) + '&type=cart&shipping_type=1',
            headers={'auth-token': 'Bearer ' + A3mall.token})
        print(confirm.json())

    order_id = None

    # 确认订单
    def test_A3mall_09(self):
        create = requests.post(url='http://10.59.9.14:7003/api/order/create',
                               headers={'auth-token': 'Bearer ' + A3mall.token},
                               json={"id": A3mall.cartid, "type": "cart", "address_id": 56, "store_id": 1,
                                     "shipping_type": 1, "bonus_id": "0",
                                     "payment": "balance", "remarks": "", "source": 1,
                                     "url": "http://10.59.9.14:7003/wap/pages/cart/confirm?id=3813&type=cart"})
        print(create.json())
        # 提取订单id
        A3mall.order_id = create.json()['data']['order_id']
        print(A3mall.order_id)

    # 订单详情
    def test_A3mall_091(self):
        print(A3mall.token)
        detail = requests.post(url='http://10.59.9.14:7003/api/order/detail',
                               headers={'auth-token': 'Bearer ' + A3mall.token},
                               json={"id": str(A3mall.order_id)})
        print(detail.json())


if __name__ == '__main__':
    unittest.main()
